#include "listmodel.h"

#include <QList>
#include "../core/core.h"
#include "../database_plugins/idatabase.h"

#include <QList>
#include <QDebug>
#include <QApplication>
#include <QFont>

class ListModel::ListModelPrivate{
public:
	struct ListEntry{
		uint id;
		int unread;
		QString name;
		QIcon favicon;
	};

	QList<ListEntry> m_channels;

};


//end of d-class definitions
//------------------------------------------------------------------------------
ListModel::ListModel(QObject *parent)
	: QAbstractListModel(parent),
	d( new ListModelPrivate() )
{

}

ListModel::~ListModel(){
	delete d;
}

int ListModel::rowCount(const QModelIndex &parent) const {
	return parent.isValid() ? 0 : d->m_channels.size();
}

QVariant ListModel::data(const QModelIndex &index, int role) const {
	switch( role ){
	case Qt::DisplayRole:
		return static_cast<const ListModelPrivate::ListEntry&>
										(d->m_channels.at( index.row() )).name;
	case (Qt::UserRole + 1):
		return static_cast<const ListModelPrivate::ListEntry&>
									  (d->m_channels.at( index.row() )).unread;
	case Qt::DecorationRole:
		return static_cast<const ListModelPrivate::ListEntry&>
									 (d->m_channels.at( index.row() )).favicon;

	}

	return QVariant();
}

void ListModel::fillModel(){
	d->m_channels.clear();

	IDatabase *db = qobject_cast<IDatabase*>(Core::instance()->database());

	if( !db ){
		qWarning() << "ListModel => database NULL";
		return;
	}

	QList<ChannelsList> list = db->getCannelsList();

	foreach(ChannelsList item, list){
		ListModelPrivate::ListEntry entry;
		entry.id = item.id;
		entry.name = item.name;
		entry.unread = item.unread;

		//TODO
		//favicon should be as file on disk,
		//e.g. %appdata%/beaver/favicons/<id>.<filetype>

		d->m_channels.push_back( entry );
	}

	reset();
}


//delegate class

ListModelDeleagate::ListModelDeleagate(QObject *parent)
	: QStyledItemDelegate(parent)
{

}

QSize ListModelDeleagate::sizeHint(const QStyleOptionViewItem &option,
								   const QModelIndex &index) const
{
	QSize size = QStyledItemDelegate::sizeHint(option, index);
	size.setHeight(qMax(size.height(), 24));
	return size;
}

void ListModelDeleagate::paint(QPainter *painter,
							   const QStyleOptionViewItem &option,
							   const QModelIndex &index) const
{
	QStyledItemDelegate::paint( painter, option, index );

	if( index.column() != 0 )
		return;

	if( qVariantCanConvert<int>( index.data(Qt::UserRole + 1 ) ) &&
		qVariantCanConvert<QString>( index.data() ) ){
		int unreaded = index.data(Qt::UserRole + 1 ).toInt();
		painter->save();
		QFont font = painter->font();
		font.setBold( true );
		painter->setFont( font );
		QApplication::style()->drawItemText( painter, option.rect,
											 Qt::AlignRight | Qt::AlignVCenter,
											 option.palette, true,
							  QString("(%1)").arg(QString::number( unreaded )));
		painter->restore();
	}
}

//end of delegate class
